﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PermissionsAllUsersController.cs" company="DTTB">
//   2011
// </copyright>
// <summary>
//   Defines the PermissionsAllUsersController type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Web.Mvc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.PhanQuyen;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Controllers.Base;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.PhanQuyen;
using DTTB.DTO;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.Controllers
{
    public class PermissionsAllUsersController : BaseController
    {
        public RolesServiceDelegate RolesServiceDelegate = new RolesServiceDelegate();
        public UsersServiceDelegate UsersServiceDelegate = new UsersServiceDelegate();
        public PermissionsAllUsersServiceDelegate PermissionsAllUsersServiceDelegate = new PermissionsAllUsersServiceDelegate();

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult SetPermissionsForAllUsers()
        {
            var form = new PermissionsAllUsersForm();

            //var dictionary = new Dictionary<string, object> { { "IsDelete", false } };

            ////Get all Permission For all User in PermissionsAllUsers Table
            form.PermissionsAllUsersList = this.PermissionsAllUsersServiceDelegate.GetAllPermissionsAllUsers();

            ////Get all Paths in database
            form.PathsList = this.PermissionsAllUsersServiceDelegate.GetPaths();

            ////Build all Paths for User from PermissionsAllUsers table:
            form.PathsForAllUsersList = new List<PathsDto>();
            for (int i = 0; i < form.PermissionsAllUsersList.Count; i++)
            {
                for (int j = 0; j < form.PathsList.Count; j++)
                {
                    if (form.PermissionsAllUsersList[i].PathId == form.PathsList[j].ID)
                    {
                        form.PathsForAllUsersList.Add(form.PathsList[j]);
                    }
                }
            }

            ////Get all Blocks in Application base on Paths table
            IList<object> blocksList = new List<object>();
            blocksList = this.UsersServiceDelegate.GetAllBlocksInPaths();
            form.Blocks = new List<string>();
            foreach (var block in blocksList)
            {
                form.Blocks.Add((string)block);
            }

            //Get all Module By block in Application base on Paths table
            form.ModulesByBlockArray = new IList<string>[form.Blocks.Count];
            int t = 0;
            foreach (string block in form.Blocks)
            {

                IList<object> moduleListByBlockTemp = UsersServiceDelegate.GetAllModulesInPathsByBlocks(block); // lấy các Module theo Block

                IList<string> moduleListByBlock = new List<string>();

                foreach (var module in moduleListByBlockTemp)// chuyen list<object> sang list<string>
                {
                    moduleListByBlock.Add((string)module);
                }

                form.ModulesByBlockArray[t++] = moduleListByBlock;
            }

            ////Get all Module in Application base on Paths table : phần cũ Version 1
            //IList<object> modules = new List<object>();
            //modules = this.PermissionsAllUsersServiceDelegate.GetAllModuleInPaths();
            //form.Modules = new List<string>();
            //foreach (var module in modules)
            //{
            //    form.Modules.Add((string)module);
            //}

            return View("PermissionsAllUsers", form);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SetPermissionsForAllUsersUpdate(PermissionsAllUsersForm form)
        {
            try
            {
                IList<PermissionsAllUsersDto> dtos = new List<PermissionsAllUsersDto>();
                string[] newPermissionsAllUserString = !string.IsNullOrEmpty(form.NewPermissionsAllUserString) ? form.NewPermissionsAllUserString.Split('|') : new string[0];
                for (int i = 1; i < newPermissionsAllUserString.Length; i++)
                {
                    if (!String.IsNullOrEmpty(newPermissionsAllUserString[i]))
                    {
                        string[] modulePermissions = newPermissionsAllUserString[i].Split('-');
                        //// query all Paths that has permissiontype and module as modulePermissions
                        if (modulePermissions.Length == 2 && !String.IsNullOrEmpty(modulePermissions[0]) &&
                            !String.IsNullOrEmpty(modulePermissions[1]))
                        {
                            string module = modulePermissions[0].Trim();
                            string permissionType = modulePermissions[1].Trim();
                            IList<PathsDto> pathsDtos =
                                this.RolesServiceDelegate.GetAllPathsByModulesAndPermissionType(module, permissionType);
                            foreach (PathsDto pathsDto in pathsDtos)
                            {
                                var permissionsAllUsersDto = new PermissionsAllUsersDto();
                                permissionsAllUsersDto.PathId = pathsDto.ID;
                                permissionsAllUsersDto.IsDelete = false;
                                permissionsAllUsersDto.CreateDate = DateTime.Now;
                                //permissionsAllUsersDto.LastUpdatedDate = DateTime.Now; //Không cần thiết
                                permissionsAllUsersDto.UpdateDate = DateTime.Now;
                                permissionsAllUsersDto.CreateBy = Convert.ToInt32(Session["USERID"]);
                                permissionsAllUsersDto.UpdateBy = Convert.ToInt32(Session["USERID"]);

                                dtos.Add(permissionsAllUsersDto);
                            }
                        }
                    }
                }

                this.PermissionsAllUsersServiceDelegate.UpdatePermissionsForAllUsers(dtos);
                form.message = "Cập nhật thành công !";
            }
            catch (Exception)
            {
                form.message = "Cập nhật thất bại, xin thử lại !";
            }

            //Get data cho View


            ////Get all Permission For all User in PermissionsAllUsers Table
            form.PermissionsAllUsersList = this.PermissionsAllUsersServiceDelegate.GetAllPermissionsAllUsers();

            ////Get all Paths in database
            form.PathsList = this.PermissionsAllUsersServiceDelegate.GetPaths();

            ////Build all Paths for User from PermissionsAllUsers table:
            form.PathsForAllUsersList = new List<PathsDto>();
            for (int i = 0; i < form.PermissionsAllUsersList.Count; i++)
            {
                for (int j = 0; j < form.PathsList.Count; j++)
                {
                    if (form.PermissionsAllUsersList[i].PathId == form.PathsList[j].ID)
                    {
                        form.PathsForAllUsersList.Add(form.PathsList[j]);
                    }
                }
            }

            ////Get all Blocks in Application base on Paths table
            IList<object> blocksList = new List<object>();
            blocksList = this.UsersServiceDelegate.GetAllBlocksInPaths();
            form.Blocks = new List<string>();
            foreach (var block in blocksList)
            {
                form.Blocks.Add((string)block);
            }

            //Get all Module By block in Application base on Paths table
            form.ModulesByBlockArray = new IList<string>[form.Blocks.Count];
            int t = 0;
            foreach (string block in form.Blocks)
            {

                IList<object> moduleListByBlockTemp = UsersServiceDelegate.GetAllModulesInPathsByBlocks(block); // lấy các Module theo Block

                IList<string> moduleListByBlock = new List<string>();

                foreach (var module in moduleListByBlockTemp)// chuyen list<object> sang list<string>
                {
                    moduleListByBlock.Add((string)module);
                }

                form.ModulesByBlockArray[t++] = moduleListByBlock;
            }

            ////Get all Module in Application base on Paths table : phần cũ version 1
            //IList<object> modules = new List<object>();
            //modules = this.PermissionsAllUsersServiceDelegate.GetAllModuleInPaths();
            //form.Modules = new List<string>();
            //foreach (var module in modules)
            //{
            //    form.Modules.Add((string)module);
            //}

            //End get Data


            return View("PermissionsAllUsers", form);
        }

    }
}
